package org.metasketch.demo.diagrams.TT;

import javafx.geometry.Orientation;
import javafx.scene.control.ToolBar;
import org.metasketch.demo.model.TransactionKind;
import org.metasketch.editor.links.GraphicalLink;
import org.metasketch.editor.links.GraphicalLinkConfig;
import org.metasketch.editor.links.LinkEndOrientation;
import org.metasketch.editor.links.LinkEndType;
import org.metasketch.editor.toolbox.DefaultDiagramBuilder;
import org.metasketch.editor.toolbox.DefaultSelectHandler;
import org.metasketch.editor.toolbox.SimpleToolBaseDiagramType;

public class TTDiagram extends SimpleToolBaseDiagramType {

	public TTDiagram() {
		super("Transaction Composition Diagram", new DefaultDiagramBuilder());

		registerTool("Select", null, "Select", new DefaultSelectHandler(), null);

		registerToolClasses("Transaction Kind", null,
				"TransactionKind-TT-Node", TransactionKind.class,
				TransactionKindNode.class, true);
		addDefaultDeleteFromDiagramGraphicalPopupMenuOption("TransactionKind-TT-Node");

		{
			GraphicalLinkConfig linkConfigA = new GraphicalLinkConfig();

			linkConfigA.setSourceTypes("TransactionKind-TT-Node");
			linkConfigA.getSource().getLinkEnd()
					.setOrientation(LinkEndOrientation.CLOSEST);
			// linkConfigA.getSource().getLinkEnd()
			// .setGraphic(LinkEndType.FILLEDTRIANGLE);

			linkConfigA.setTargetTypes("TransactionKind-TT-Node");
			linkConfigA.getTarget().getLinkEnd()
					.setOrientation(LinkEndOrientation.CLOSEST);
			linkConfigA.getTarget().getLinkEnd()
					.setGraphic(LinkEndType.TRIANGLE);

			linkConfigA
					.setScript("sourceSemantic.createCompleteCompositionLink(targetSemantic);");

			registerDefaultGraphicalLink("composition link",
					"composition-link", null, linkConfigA);
		}

		{
			GraphicalLinkConfig linkConfigA = new GraphicalLinkConfig();

			linkConfigA.setSourceTypes("TransactionKind-TT-Node");
			linkConfigA.getSource().getLinkEnd()
					.setOrientation(LinkEndOrientation.CLOSEST);
			// linkConfigA.getSource().getLinkEnd()
			// .setGraphic(LinkEndType.FILLEDTRIANGLE);

			linkConfigA.setTargetTypes("TransactionKind-TT-Node");
			linkConfigA.getTarget().getLinkEnd()
					.setOrientation(LinkEndOrientation.CLOSEST);
			linkConfigA.getTarget().getLinkEnd()
					.setGraphic(LinkEndType.TRIANGLE);

			linkConfigA
					.setScript("sourceSemantic.createEnterCompositionLink(targetSemantic);");

			GraphicalLink link = registerDefaultGraphicalLink(
					"composition enter link", "composition-enter-link", null,
					linkConfigA);

			link.setStyle("-fx-stroke: green; -fx-stroke-width: 2;   -fx-stroke-dash-array: 12 6;    -fx-stroke-dash-offset: 6;     -fx-stroke-line-cap: butt;");
		}

		{
			GraphicalLinkConfig linkConfigA = new GraphicalLinkConfig();

			linkConfigA.setSourceTypes("TransactionKind-TT-Node");
			linkConfigA.getSource().getLinkEnd()
					.setOrientation(LinkEndOrientation.CLOSEST);
			// linkConfigA.getSource().getLinkEnd()
			// .setGraphic(LinkEndType.FILLEDTRIANGLE);

			linkConfigA.setTargetTypes("TransactionKind-TT-Node");
			linkConfigA.getTarget().getLinkEnd()
					.setOrientation(LinkEndOrientation.CLOSEST);
			linkConfigA.getTarget().getLinkEnd()
					.setGraphic(LinkEndType.TRIANGLE);

			linkConfigA
					.setScript("targetSemantic.createExitCompositionLink(sourceSemantic);");

			GraphicalLink link = registerDefaultGraphicalLink(
					"composition exit link", "composition-exit-link", null,
					linkConfigA);

			link.setStyle("-fx-stroke: red; -fx-stroke-width: 2;   -fx-stroke-dash-array: 12 6;    -fx-stroke-dash-offset: 6;     -fx-stroke-line-cap: butt;");
		}

		{

			GraphicalLinkConfig linkConfigA = new GraphicalLinkConfig();

			linkConfigA.setSourceTypes("TransactionKind-TT-Node");
			linkConfigA.getSource().getLinkEnd()
					.setOrientation(LinkEndOrientation.CLOSEST);

			linkConfigA.setTargetTypes("TransactionKind-TT-Node");
			linkConfigA.getTarget().getLinkEnd()
					.setOrientation(LinkEndOrientation.CLOSEST);

			linkConfigA.getTarget().getLinkEnd().setGraphic(LinkEndType.ARROW);

			// linkConfigA.setS

			linkConfigA
					.setScript("sourceSemantic.createConditionalLink(targetSemantic);");

			GraphicalLink link = registerDefaultGraphicalLink(
					"conditional link", "conditional-link", null, linkConfigA);

			link.setStyle("-fx-stroke: black; -fx-stroke-width: 2;   -fx-stroke-dash-array: 12 6;    -fx-stroke-dash-offset: 6;     -fx-stroke-line-cap: butt;");

		}

		// addDefaultDeleteFromDiagramGraphicalPopupMenuOption("composition-link");
		// addDefaultDeleteFromDiagramGraphicalPopupMenuOption("conditional-link");

		addGraphicalMenuEntry(
				"composition-link",
				"Delete from Model",
				"sourceSemantic.removeCompositionLink(targetSemantic); graphic.removeFromDiagram();");

		addGraphicalMenuEntry(
				"composition-enter-link",
				"Delete from Model",
				"sourceSemantic.removeEnterCompositionLink(targetSemantic); graphic.removeFromDiagram();");

		addGraphicalMenuEntry(
				"composition-exit-link",
				"Delete from Model",
				"targetSemantic.removeExitCompositionLink(sourceSemantic); graphic.removeFromDiagram();");

		addGraphicalMenuEntry(
				"conditional-link",
				"Delete from Model",
				"sourceSemantic.removeConditionalLink(targetSemantic); graphic.removeFromDiagram();");

		// addGraphicalMenuEntry(
		// "composition-link",
		// "Reverse",
		// "sourceSemantic.reversCompositionLink(targetSemantic); graphic.removeFromDiagram();");
		//
		// addGraphicalMenuEntry(
		// "conditional-link",
		// "Reverse",
		// "sourceSemantic.removeConditionalLink(targetSemantic); graphic.removeFromDiagram();");

	}

	@Override
	public ToolBar createToolbar() {

		ToolBar toolBar = new ToolBar(getTypeButton("Select"),
				getTypeButton("TransactionKind-TT-Node"),
				getTypeButton("composition-link"),
				getTypeButton("composition-enter-link"),
				getTypeButton("composition-exit-link"),
				getTypeButton("conditional-link"));

		toolBar.setOrientation(Orientation.VERTICAL);

		// toolBar.set
		// TODO setup action handling

		return toolBar;
	}

}
